﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BookstoreDesktopClient
{
    public partial class AddOrder : Form
    {
        //BranchWS.BranchWS branch;
        BookstoreClient bookstoreClient;
        List<BranchWS.orderItem> orderItemList;
        BranchWS.order order;
        BranchWS.client client;
        BranchWS.BranchWS ordersCollection;
        string sessionID;

        public AddOrder(BookstoreClient bookstoreClient, double orderSum, List<BranchWS.orderItem> orderItemList, string sessionID, BranchWS.BranchWS ordersCollection)
        {
            InitializeComponent();
            this.bookstoreClient = bookstoreClient;
            this.orderItemList = orderItemList;
            this.ordersCollection = ordersCollection;
            this.sessionID = sessionID;

            this.netPriceLabel.Text = "Net price: " + Math.Round((orderSum - 0.23 * orderSum),2) + " PLN";
            this.vatLabel.Text = "Tax price: " + Math.Round((0.23 * orderSum),2) + " PLN";
            this.sumLabel.Text = "Total sum: " + orderSum + " PLN";
        }

        private void addOrderButton_Click(object sender, EventArgs e)
        {
            order = new BranchWS.order();
            client = new BranchWS.client();
            client.id = ordersCollection.getfirstavailableClientid(sessionID);
            client.name = clientNameTextBox.Text;
            client.surname = clientSurnameTextBox.Text;
            client.phoneNumber = Convert.ToInt64(clientPhoneTextBox.Text);
            client.email = clientEmailTextBox.Text;
            order.client = client;
            BranchWS.orderItem[] items = new BranchWS.orderItem[orderItemList.Count];
            for (int i = 0; i < orderItemList.Count; i++) items[i] = orderItemList[i];
            order.items = items;
            order.id = ordersCollection.getfirstavailableOrderid(sessionID);

            ordersCollection.addorderAsync(order, sessionID);
            ordersCollection.addorderCompleted += new BranchWS.addorderCompletedEventHandler(ordersCollection_addorderCompleted);
            this.Dispose(false);
        }

        void ordersCollection_addorderCompleted(object sender, BranchWS.addorderCompletedEventArgs e)
        {
            if (e.@return)
            {
                MessageBox.Show("Order has been added", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                bookstoreClient.showAllOrdersButton_Click(sender, e);
                
            }
            else
            {
                MessageBox.Show("Order has not been added, an error occured: \n" + e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Dispose(false);
        }
    }
}
